package org.icdio.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import sun.security.util.Resources;

/**
 * 鏁版嵁搴撹繛鎺ュ伐鍏风被
 * @author gjt
 *
 */
public class DBUtils {
	
	private static String url = "";
	
	private static String driver = "";
	
	private static String userName = "";
	
	private static String password = "";
	
	// 
	static {
		
		Properties prop = new Properties();
		
		BufferedInputStream buff = null;
		
		try {
			buff = new BufferedInputStream(DBUtils.class.getResourceAsStream("/org/icdio/config/config.properties"));
			prop.load(buff);
		}catch(Exception e) {
            System.out.println("==发生错误=="); 
			e.printStackTrace();
		} finally {
			if(null != buff) {
				try {
					buff.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		driver = prop.getProperty("driver");
		
		url = prop.getProperty("url");
		
		userName = prop.getProperty("User");
		
		password = prop.getProperty("password");
	
	}
	
//	public static void main(String[] args) {
//		Connection conn  = open();
//		if(conn != null) {
//			System.out.println("杩炴帴鎴愬姛锛�");
//		}else {
//			System.out.println("杩炴帴澶辫触锛�");
//		}
//	}

	public static Connection open() {
		try {
			Class.forName(driver);
			return DriverManager.getConnection(url,userName,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void close(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	} 

}
